package com.ww.springboot.boot.swing;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.BeanUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 描述：
 * <p>
 * 写一个通用打印的类
 *
 * @author 🧑 ‍wanwei
 * @since 2023-06-10 09:12
 */
public class SwingUtil extends JFrame {

    private static boolean flag = true;

    private static String title;

    private static java.util.List<Object> list;

    public SwingUtil(String title) throws HeadlessException {
        this.title = title;
        add(new Composition());
    }


    static class Composition extends JPanel {

        public Composition() {
            Timer timer = new Timer(100, new Refresh());
            timer.start();
        }


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = -290;
            int y = 170;
            //
            g.drawString(title, 300, 100);
            for (int index = 0; index < list.size(); index++) {
                Object o = list.get(index);
                x += 300;

                g.drawRect(x, 150, 300, 300);
                g.drawString(String.valueOf(index), x + 145, 150);
                //字符串
                paint(g, x, y, o, 0);
            }
        }

        private void paint(Graphics g, int x, int y, Object o, int level) {
            if (o.getClass().isArray()) {
                this.paintArray(g, x, y, o, level + 1);
            } else if (BeanUtils.isSimpleValueType(o.getClass())) {
                this.paintString(g, x, y, String.valueOf(o));
            }
        }

        private void paintArray(Graphics g, int x, int y, Object o, int level) {
            int length = Array.getLength(o);
            for (int i = 0; i < length; i++) {
                Object o1 = Array.get(o, i);
                if (level == 1) {
                    paint(g, x, y + i * 30, o1, level);
                } else {
                    paint(g, x + i * 30, y, o1, level);
                }
            }
        }

        private void paintString(Graphics g, int x, int y, String o) {
            g.drawString(String.valueOf(o), x, y);
        }


        class Refresh implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //重画
                repaint();
            }
        }

    }

    public static void refreshData(long millis, Object... objects) {
        if (flag) {
            SwingUtil frame = new SwingUtil("测试");
            frame.setTitle("通用绘图");
            frame.setSize(1000, 600);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            flag = false;
        }
        list = Arrays.asList(objects);
        try {
            Thread.sleep(millis);
        } catch (Exception e) {

        }
    }
}
